package ru.stepanovdn.trading.server.service.tinkoff

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.stereotype.Service
import ru.stepanovdn.trading.server.service.dictionary.InstrumentService
import java.math.BigDecimal

@Service
@ConditionalOnProperty(name = ["tinkoff.emulator"], havingValue = "true", matchIfMissing = true)
class TinkoffMockAPI : ATinkoffAPI() {
    override fun buy(instrument: InstrumentService.Instrument, price: BigDecimal, count: Int): ITinkoffOrderAPI.OrderResult {
        return ITinkoffOrderAPI.OrderResult(calculateCommission(price, count), price)
    }

    override fun sell(instrument: InstrumentService.Instrument, price: BigDecimal, count: Int): ITinkoffOrderAPI.OrderResult {
        return ITinkoffOrderAPI.OrderResult(calculateCommission(price, count), price)
    }

    private fun calculateCommission(price: BigDecimal, count: Int): BigDecimal {
        return price.multiply(PERCENT).multiply(BigDecimal.valueOf(count.toLong()))
    }

    companion object {
        private val PERCENT: BigDecimal = BigDecimal("0.0005")
    }
}
